package com.zjut.zs.util.tools;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zjut.an.admin.dao.AnAdmin;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.log.dao.AnLog;
import com.zjut.an.log.manager.AnLogManager;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.DateUtil;
import com.zjut.zs.school.dao.ZsSchool;
import com.zjut.zs.school.manager.ZsSchoolManager;
import com.zjut.zs.userstatus.dao.ZsUserStatus;
import com.zjut.zs.userstatus.manager.ZsUserStatusManager;

public class JobQuartzUtil {

	@Resource(name = "DateUtil")
	DateUtil dateutil;

	@Resource(name = "AnConfigManager")
	private AnConfigManager anconfig;
	
	@Resource(name = "ZsSchoolManager")
	private ZsSchoolManager smanager;
	
	@Resource(name = "ZsUserStatusManager")
	private ZsUserStatusManager stmanager;

	@Resource(name = "AnLogManager")
	private AnLogManager logmanager;
	
	private static final Logger log = LoggerFactory.getLogger(JobQuartzUtil.class);
	
	public void work() {
		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!work!!!!!!!!!!!!!!!!!!!!!!");
		if (anconfig.getConfigInfoByName("系统开始时间")!=null && anconfig.getConfigInfoByName("系统结束时间")!=null ) {
			long start = Long.valueOf(anconfig.getConfigInfoByName("系统开始时间").getCoValue().trim());
			long end = Long.valueOf(anconfig.getConfigInfoByName("系统结束时间").getCoValue().trim());
			long now = Long.valueOf(dateutil.getCurrentDate4());			
			ZsSchool school = new ZsSchool();
			school = (ZsSchool)smanager.getDataByInstance(1,1,null,null,school).getObjects().get(0);
			System.out.println("### start:"+start);
			System.out.println("### end:"+end);
			System.out.println("### now:"+now);
			if (start < now && now < end) {		
				System.out.println("### 开启注册!");		
				school.setScRegActive(Status.ON.getStatus());
				school.setScAppActive(Status.ON.getStatus());
				school.setScFilesendActive(Status.ON.getStatus());
				smanager.updateData(school);
			} else {
				System.out.println("### 关闭注册!");
				school.setScRegActive(Status.OFF.getStatus());
				school.setScAppActive(Status.OFF.getStatus());
				school.setScFilesendActive(Status.OFF.getStatus());
				smanager.updateData(school);
			}
		}		
		if ("是".equals(anconfig.getSingleConfigInfoByName("退回申请自动退回"))) {
			ZsUserStatus userstatus = new ZsUserStatus();
			userstatus.setSendStatus(Status.SENDSTATUS_BACK.getStatus());
			List list = stmanager.getDataByInstance(1, 100, null, null, userstatus).getObjects();
			for (int i=0; i<list.size(); i++) {
				userstatus = (ZsUserStatus)list.get(i);
				stmanager.sendStatus(userstatus,Status.SENDSTATUS_NO.getStatus());
				stmanager.updateData(userstatus);
				AnLog log = new AnLog(userstatus.getZsUser().getUsId(), "报考日志","系统自动"+Status.SENDSTATUS_NO.getStatus()+userstatus.getUsIdcard()+"于"+dateutil.getCurrentDate5(), dateutil.getCurrentDate5());
				logmanager.addData(log);
			}
		}
		if ("是".equals(anconfig.getSingleConfigInfoByName("退回申请自动退回"))) {
			ZsUserStatus userstatus = new ZsUserStatus();
			userstatus.setAppStatus(Status.APPSTATUS_BACK.getStatus());
			List list = stmanager.getDataByInstance(1, 100, null, null, userstatus).getObjects();
			for (int i=0; i<list.size(); i++) {
				userstatus = (ZsUserStatus)list.get(i);
				stmanager.appStatus(userstatus,Status.APPSTATUS_NO.getStatus());
				stmanager.updateData(userstatus);
				AnLog log = new AnLog(userstatus.getZsUser().getUsId(), "报考日志","系统自动"+Status.APPSTATUS_NO.getStatus()+userstatus.getUsIdcard()+"于"+dateutil.getCurrentDate5(), dateutil.getCurrentDate5());
				logmanager.addData(log);
			}
		}
	}
}
